<?php
declare (strict_types=1);

namespace app\admin\model\base;

use app\BaseModel;
use aliyun\AliyunOss;

/**
 * 管理员表模型
 */
class Admin extends BaseModel
{

    /**
     * 获取管理员列表
     * @param array $where
     * @param int $page
     * @param int $limit
     * @param string $orderby
     * @return array
     */
    public static function getList(array $where, int $page, int $limit, string $orderby = ''): array
    {
        $data = self::where($where)
            ->with(['special','duties'])
            ->order($orderby)
            ->paginate(['list_rows' => $limit, 'page' => $page], false)
            ->toArray();
        $AliyunOss = new AliyunOss();
        //遍历数组
        foreach ($data['data'] as $k => &$v) {
            unset($v['safe_code']);
            unset($v['password']);
            $v['avatar'] = $v['avatar'] ?: config('ws_config.avatar');
            $v['avatar'] = $AliyunOss->getObjectUrl($v['avatar']);
        }
        return $data;
    }

    /**
     * 关联专业
     * @return \think\model\relation\HasOne
     */
    public function special(){
        return $this->hasOne(Special::class,'id','special_id')->field('id,name');
    }

    /**
     * 关联专业
     * @return \think\model\relation\HasOne
     */
    public function duties(){
        return $this->hasOne(Duties::class,'id','duties_id')->field('id,name');
    }
}




